import {Pipe, PipeTransform} from '@angular/core';
import {DictService} from '../service/dict.service';
import {Observable} from 'rxjs';

/**
 * 描述：处理获取字典描述，有缓存
 * 作者：xc
 * 时间：2020-08-13 10:33
 */
@Pipe({
  name: 'dictDesc'
})
export class DictDescPipe implements PipeTransform {

  constructor(private dictService: DictService) {
  }

  transform(value: any, dictType?: string, options?: Array<any>): any {
    if (options) {
      return new Observable((observer) => {
        let bool = true;
        for (const option of options) {
          if (option.value + '' === value + '') {
            bool = false;
            observer.next(option.name);
          }
        }
        if (bool) {
          observer.next(value);
        }
      });
    } else {
      if (dictType) {
        return this.dictService.getCacheAndDictDescByType(value, dictType);
      } else {
        return new Observable((observer) => {
          observer.next(value);
        });
      }
    }
  }
}
